﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.Modeling;

namespace UofCASE.AgileProductLinerDSL {
    public static partial class TestConnectBuilder {

        public static void Connect(ModelElement sourceElement, ModelElement targetElement) {
            if (!(sourceElement is Test || sourceElement is Feature)) return;
            if (!(targetElement is Test || targetElement is Feature)) return;
            Test test = targetElement as Test;

            if (test == null) {
                ConnectLogic(targetElement, sourceElement as Test);
            } else ConnectLogic(sourceElement, test);
        }

        private static void ConnectLogic(ModelElement featureElement, Test test) {
            Feature feature = featureElement as Feature;
            TestPort testPort;

            if (feature != null) {
                testPort = feature.TestPort;
            } else {
                testPort = featureElement as TestPort;
            }


            if (testPort == null) {
                testPort = new TestPort(feature.Partition);
                FeatureHasTestPort featureTestPortCon = new FeatureHasTestPort(feature, testPort);

            }

            FeaturePort featurePort = new FeaturePort(test.Partition);
            test.FeaturePorts.Add(featurePort);
            featurePort.Test = test;

            TestPortReferencesFeaturePorts con = new TestPortReferencesFeaturePorts(testPort, featurePort);
        }

        public static bool CanAcceptSource(ModelElement sourceElement) {
            return sourceElement is Feature || sourceElement is TestPort || sourceElement is Test;
        }

        public static bool CanAcceptSourceAndTarget(ModelElement sourceElement, ModelElement targetElement) {
            Test test = targetElement as Test;

            if (test == null){
                Test test2 = sourceElement as Test;
                if (test2 == null) return false;
                return CanAcceptSourceAndTargetLogic(targetElement, test2);
            } else if (sourceElement is Test) {
                return false;
            } else return CanAcceptSourceAndTargetLogic(sourceElement, test);

        }

        private static bool CanAcceptSourceAndTargetLogic(ModelElement featureElement, Test target) {
            Feature feature = featureElement as Feature;
            TestPort testPort;

            if (feature != null) {
                testPort = feature.TestPort;
                if (testPort == null) return true;
            } else {
                testPort = featureElement as TestPort;
                if (testPort == null) return false;
            }

            

            foreach (FeaturePort featurePort in testPort.FeaturePorts) {
                if (target.FeaturePorts.Contains(featurePort)) {
                    System.Diagnostics.Debug.WriteLine("AlreadyThereFalse");
                    return false;
                }
            }
            return true;
        }
    }
}
